<%--
  -	$Version: 0.1 $
  -	$Date: 2008-09-09 16:48:22 $
--%>
<%@ page pageEncoding="utf-8"%>
<%@ page import="org.jawa.util.JawaGlobals"%>
<%@ page import="org.jawa.JawaServer"%>
<%@ page import="org.jawa.util.ParamUtils,org.jawa.util.ClassUtils,org.jawa.database.*,java.io.File,java.sql.*,java.util.HashMap,java.util.Locale,java.util.Map,java.util.StringTokenizer"%>

<%
	// 系统已安装
	if (!JawaServer.getInstance().isSetupMode()) {
        response.sendRedirect("setup-completed.jsp");
        return;
    }
%>

<%!
    static final String STANDARD = "standard";
	static final String EMBEDDED = "embedded";
	
	boolean testConnection(Map<String, String> errors) {
		boolean success = true;
		Connection con = null;
		try {
			con = DbConnectionManager.getConnection();
			if (con == null) {
				success = false;
				errors.put("general", "A connection to the database could not be "
						+ "made. View the error message by opening the " + "\"" + File.separator + "logs"
						+ File.separator + "error.log\" log " + "file, then go back to fix the problem.");
			} else {
				// See if the Jive db schema is installed.
				try {
					Statement stmt = con.createStatement();
					// Pick an arbitrary table to see if it's there.
					stmt.executeQuery("SELECT 1 FROM app_user");
					stmt.close();
				} catch (SQLException sqle) {
					success = false;
					errors.put("general", "The Openfire database schema does not "
							+ "appear to be installed. Follow the installation guide to " + "fix this error.");
				}
			}
		} catch (Exception ignored) {
		} finally {
			try {
				con.close();
			} catch (Exception ignored) {
			}
		}
		return success;
	}
%>
<%
	// 获取参数
	String mode = ParamUtils.getParameter(request, "mode");
	boolean next = ParamUtils.getBooleanParameter(request, "next");

	Map<String, String> errors = new HashMap<String, String>();
	if (next) {
		if (STANDARD.equals(mode)) {
			response.sendRedirect("setup-datasource-standard-settings.jsp");
			return;
		} else if (EMBEDDED.equals(mode)) {
			JawaGlobals.setXMLProperty("database.connectionProvider.className", "org.jawa.database.EmbeddedConnectionProvider");
			if (testConnection(errors)) 
			{
				response.sendRedirect("setup-clustering-settings.jsp");
				return;
			}
		}
	}

	// Defaults
	if (mode == null) {
		// If the "embedded-database" directory exists, select to the embedded db as the default.
		if (new java.io.File(JawaGlobals.getHomeDirectory(), "conf/database").exists()) {
			mode = EMBEDDED;
		}
		// Otherwise default to standard.
		else {
			mode = STANDARD;
		}
	}
%>

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>数据库设置</title>
		<meta name="currentStep" content="2" />
	</head>
	<body>
		<div class="row-fluid">
			<div class="page-header">
				<h3>
					数据库设置
				</h3>
			</div>
			<%  if (errors.size() > 0) { %>
			    <div class="alert alert-error fade in">
					<i class="icon-remove-circle icon-white "></i><strong>错误 － <%= errors.get("general") %></strong>
				</div>
			<%  } %>
			<form action="setup-datasource-settings.jsp" method="post" class="">
					<input type="hidden" name="next" value="true">
					<table class=" table well" >
						<tr>
							<td>
								<input type="radio" name="mode" value="<%=STANDARD%>" id="rb02" <%=((STANDARD.equals(mode)) ? "checked" : "")%> />
								<strong>
									标准数据库连接
								</strong>
								<input type="radio" name="mode" value="<%=EMBEDDED%>" id="rb01"  <%=((EMBEDDED.equals(mode)) ? "checked" : "")%> />
								<strong>
									内置数据库
								</strong>
							</td>
						</tr>
					</table>
					<div class="form-actions" >
						<input type="button" value="上一步" id="jawa-setup-prev" class="btn btn-info " onclick="window.location.href='index.jsp'" />
						<input type="submit" name="continue" value="下一步" id="jawa-setup-next" class="btn btn-primary " />
					</div>

			</form>
		</div>
	</body>
</html>
